package mapReduce.writable;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class GroupGenderWritable implements WritableComparable<GroupGenderWritable> {

    private String groupName = "";
    private String userName = "";
    private int age;

    @Override
    public int compareTo(GroupGenderWritable o) {

        String thisGroupName = this.groupName;
        String thatGroupName = o.groupName;

        String thisUserName = this.userName;
        String thatUserName = o.userName;

        int thisAge = this.age;
        int thatAge = o.age;

        if(thisGroupName.equals(thatGroupName)){
            if(thisUserName.equals(thatUserName)){
                return thisAge-thatAge;
            }else{
                return thisUserName.compareTo(thatUserName);
            }
        }else{
            return thisGroupName.compareTo(thatGroupName);
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(groupName);
        out.writeUTF(userName);
        out.writeInt(age);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.groupName = in.readUTF();
        this.userName = in.readUTF();
        this.age = in.readInt();
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
